package cn.gbase.jiangsu.data.transfer.action;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.nutz.dao.QueryResult;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

import cn.gbase.jiangsu.data.transfer.bean.Community;
import cn.gbase.jiangsu.data.transfer.bean.Company;
import cn.gbase.jiangsu.data.transfer.bean.CompanyUser;
import cn.gbase.jiangsu.data.transfer.bean.ProductOrder;
import cn.gbase.jiangsu.data.transfer.service.CompanyService;
import cn.gbase.jiangsu.data.transfer.service.CompanyUserService;
import cn.gbase.jiangsu.data.transfer.service.PrintInfoService;
import cn.gbase.jiangsu.data.transfer.service.SupplierService;
import cn.gbase.jiangsu.data.transfer.service.TFileService;

@IocBean
@At("/printInfoAction")
public class PrintInfoAction {
	@Inject
	private CompanyUserService companyUserService;

	@Inject
	private PrintInfoService printInfoService;

	@Inject
	private CompanyService companyService;

	@Inject
	private TFileService tFileService;

	@Inject
	private SupplierService supplierService;

	//不用了
	@At("/printSmallBillList")
	@Ok("jsp:/printInfo/printSmallBillPageList")
	public Object printSmallBillList(@Param("p") int currentPage, @Param("orderCode") String orderCode,
			@Param("supName") String supName, @Param("status") String status, @Param("startTime") String startTime,
			@Param("endTime") String endTime, HttpServletRequest request, HttpSession session) {
		return null;
	}

	@At("/printSmallBill/*")
	@Ok("jsp:/printInfo/printSmallBillPage")
	public Object printSmallBill(String code, HttpSession session, HttpServletRequest request) {
		CompanyUser loginUser = (CompanyUser) session.getAttribute("companyUser");
		Company company = companyService.getCompany(loginUser.getCompanyId());
		Community community = companyUserService.getCommunity(company.getCommunityId());
		Map resultMap = new HashMap();
		resultMap = printInfoService.printSmallBillService(code);
		request.setAttribute("companyCode", community.getCommunityCode());
		request.setAttribute("companyName", company.getCompanyName());
		request.setAttribute("code", code);
		request.setAttribute("orderSupplierViewList", resultMap.get("resultMap"));
		//转换订单日期和送达日期
		DateFormat formatNew = new SimpleDateFormat("yyyy年MM月dd日");
		if (resultMap.get("createDate") != null) {
			request.setAttribute("createTime", formatNew.format(resultMap.get("createDate")));
		}
		if (resultMap.get("actualArriveDate") != null) {
			request.setAttribute("actualArriveDate", formatNew.format(resultMap.get("actualArriveDate")));
		}
		return resultMap;
	}

	/**
	 * 下载打印控件
	 * 
	 * @param ids
	 * @return
	 */
	@At("/downloadLodop")
	@Ok("void")
	public void downloadLodop(HttpServletResponse response) throws IOException {
		response.setContentType("application/octet-stream");
		response.setContentType("application/OCTET-STREAM;charset=UTF-8");
		response.setHeader("Content-Disposition", "attachment;filename=" + "Lodop6.198.midownload.zip");

		File file = new File("D:\\geyun\\Lodop6.198.midownload.zip");

		FileInputStream fis = new FileInputStream(file);
		BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream());
		byte[] buffer = new byte[1024];
		int len;
		while ((len = fis.read(buffer)) != -1) {
			out.write(buffer, 0, len);
			out.flush();
		}
	}

	//
	@At("/orderDetailPrint/*")
	@Ok("jsp:/printInfo/orderDetailPrint")
	public QueryResult orderDetailPrint(String orderCode, HttpServletRequest request) {
		Map<String, String> itemMap = new HashMap<String, String>();
		itemMap.put("orderCode", orderCode);
		request.setAttribute("formdata", itemMap);
		ProductOrder goodsOrder = supplierService.getGoodsOrder(Integer.valueOf(orderCode));
		request.setAttribute("total", goodsOrder.getProductPrice());
		return supplierService.getOrderDetail(orderCode);
	}
}
